package com.shqcjd.preinstallmobile.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.shqcjd.preinstallmobile.R;
import com.shqcjd.preinstallmobile.api.ApiService;
import com.shqcjd.preinstallmobile.helper.rxjavahelper.RxObserver;
import com.shqcjd.preinstallmobile.helper.rxjavahelper.RxSchedulersHelper;
import com.shqcjd.preinstallmobile.model.ResponseData;
import com.shqcjd.preinstallmobile.model.eventbus.RefreshReddotEvent;
import com.shqcjd.preinstallmobile.model.pojo.MsgBean;
import com.shqcjd.preinstallmobile.ui.adapter.MessageListAdapter;
import com.shqcjd.preinstallmobile.ui.base.BaseActivity;
import com.shqcjd.preinstallmobile.ui.base.BasePresenter;
import com.shqcjd.preinstallmobile.util.ToastUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.disposables.Disposable;

/**
 * 消息列表
 */
public class MessageListActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.RequestLoadMoreListener {
    private static final int PAGE_SIZE = 10;
    private int mNextRequestPage = 1;

    @BindView(R.id.iv_toolbar_back)
    ImageView ivToolbarBack;
    @BindView(R.id.tv_toolbar_center)
    TextView tvToolbarCenter;
    @BindView(R.id.rv_msg_list)
    RecyclerView rvMsgList;
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout swipeLayout;
    private MessageListAdapter messageListAdapter;
    private Context mContext;
    private View notDataView;

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_message_list;
    }

    @Override
    public void initView() {
        tvToolbarCenter.setText("系统消息");
        mContext = this;
        //设置首页消息红点消失
        EventBus.getDefault().postSticky(new RefreshReddotEvent(false));
        notDataView = getLayoutInflater().inflate(R.layout.layout_no_data, (ViewGroup) rvMsgList.getParent(), false);

        initAdapter();
    }

    private void initAdapter() {
        messageListAdapter = new MessageListAdapter();
        rvMsgList.setLayoutManager(new LinearLayoutManager(this));
        rvMsgList.setAdapter(messageListAdapter);
        messageListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent equipmentIntent = new Intent(mContext, EquipmentListActivity.class);
                equipmentIntent.putExtra("jpush", true);
                startActivity(equipmentIntent);
            }
        });

        swipeLayout.setOnRefreshListener(this);
        messageListAdapter.setOnLoadMoreListener(this,rvMsgList);
    }

    @Override
    public void initData() {
        refresh();
    }


    private void refresh(){
        mNextRequestPage = 1;
        messageListAdapter.setEnableLoadMore(false);//这里的作用是防止下拉刷新的时候还可以上拉加载
        ApiService.getMsgList(mNextRequestPage,PAGE_SIZE)
                .compose(RxSchedulersHelper.io_main())
                .subscribe(new RxObserver<ResponseData<MsgBean>>() {
                    @Override
                    public void _onSubscribe(Disposable d) {
                        super._onSubscribe(d);
                        showRefreshView(true);
                    }

                    @Override
                    public void _onNext(ResponseData<MsgBean> retData) {
                        if (retData != null && retData.retCode == 10000) {
                            setData(true, retData.data.list);
                        } else {
                            ToastUtils.showShort(mContext, retData.retMsg);
                        }
                    }

                    @Override
                    public void _onError(String errorMessage) {
                        ToastUtils.showShort(mContext, errorMessage);
                        messageListAdapter.setEnableLoadMore(true);
                        showRefreshView(false);
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                        messageListAdapter.setEnableLoadMore(true);
                        showRefreshView(false);
                    }
                });
    }

    private void loadMore(){
        ApiService.getMsgList(mNextRequestPage,PAGE_SIZE)
                .compose(RxSchedulersHelper.io_main())
                .subscribe(new RxObserver<ResponseData<MsgBean>>() {

                    @Override
                    public void _onNext(ResponseData<MsgBean> retData) {
                        if (retData != null && retData.retCode == 10000) {
                            boolean isRefresh =mNextRequestPage ==1;
                            setData(isRefresh, retData.data.list);
                        } else {
                            ToastUtils.showShort(mContext, retData.retMsg);
                        }
                    }

                    @Override
                    public void _onError(String errorMessage) {
                        ToastUtils.showShort(mContext, errorMessage);
                        messageListAdapter.loadMoreFail();
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                    }
                });
    }

    public void showRefreshView(Boolean refresh) {
        swipeLayout.post(new Runnable() {
            @Override
            public void run() {
                swipeLayout.setRefreshing(refresh);
            }
        });
    }

    private void setData(boolean isRefresh, List data) {

        mNextRequestPage++;
        final int size = data == null ? 0 : data.size();
        if (isRefresh) {
            if (size > 0) {
                messageListAdapter.setNewData(data);
            } else {
                messageListAdapter.setNewData(null);
                messageListAdapter.setEmptyView(notDataView);
            }
        } else {
            if (size > 0) {
                messageListAdapter.addData(data);
            }
        }
        if (size < PAGE_SIZE) {
            //第一页如果不够一页就不显示没有更多数据布局
            messageListAdapter.loadMoreEnd(isRefresh);
        } else {
            messageListAdapter.loadMoreComplete();
        }
    }

    @OnClick(R.id.iv_toolbar_back)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void onRefresh() {
        refresh();
    }

    @Override
    public void onLoadMoreRequested() {
        loadMore();
    }
}
